﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.Web.Mvc;
using System.Web.Security;
using Bzure.Security;
using Bzure.Security.Permissions;
using Bzure.Security.Principal;
using Bzure.Apps.Security.ViewModels;
using Bzure.Common.Core;
using Bzure.Common.Text;
using Bzure.Apps.Security.ResourceFiles;
using Bzure.Common.Patterns;


namespace Bzure.Apps.Security.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "Role"), ExportMetadata("Area", "SecurityApp")]
    [Authorize]
    [SecurityAppAuthorize(RequiredRight = AdministrationRights.RoleManage)]
    public class RoleController : T4ViewController
    {
        MasterViewModel MasterViewModel = new ViewModels.MasterViewModel()
        {
            Title = "通用权限管理"
        };
        [HttpGet]
        [SecurityAppFilter(PageControllerItems.NewRoleController)]
        public ActionResult NewRole(string id)
        {
            NewRoleViewModel ViewModel = new NewRoleViewModel()
            {
                Url = Url.RouteUrl("SecurityApp_Role", new { Action = "CreateRole" }),
                Title = "创建角色",
                MasterViewModel = this.MasterViewModel
            };
            return Result(ViewModel);
        }
        [HttpPost]
        [SecurityAppFilter(PageControllerItems.NewRoleController)]
        public ActionResult CreateRole(string id, string RoleName, string RoleDescription, FormCollection Collection)
        {
            TeamRole role = Actor.Public.CreateRole(RoleName, RoleDescription);
            NotificationViewModel notification = new NotificationViewModel();

            if (role.IsNotNull())
            {
                notification.Data = NotificationData.YES;
                notification.Content = string.Format("角色{0}创建成功", RoleName);
                notification.NotificationType = NotificationType.Success;
            }
            else
            {
                notification.Data = NotificationData.NO;
                notification.Content = string.Format("创建角色失败,可能{0}角色已经存在", RoleName);
                notification.NotificationType = NotificationType.Error;
            }
            return Result(notification);
        }
        [SecurityAppFilter(PageControllerItems.RoleListController)]
        public ActionResult RoleList()
        {
            ListRoleViewModel ViewModel = new ListRoleViewModel()
            {
                MasterViewModel = this.MasterViewModel,
                Title = "角色列表",
                Url = Url.RouteUrl("SecurityApp_Role", new { Action = "DeleteRole" }),
                Roles = Actor.Public.AsRoleManager().GetAllTeamRoles()
            };
            return Result(ViewModel);
        }
        [SecurityAppFilter(PageControllerItems.DeleteRoleController)]
        public ActionResult DeleteRole(string id)
        {

            TeamRole role = Actor.Public.GetRole(id);
            NotificationViewModel notification = new NotificationViewModel();
            if (role.IsNotNull())
            {
                Actor.Public.DeleteRole(id, (bool ok) =>
                {
                    if (ok)
                    {
                        notification.Data = NotificationData.YES;
                        notification.Content = string.Format("角色:{0}删除成功", role.Name);
                        notification.NotificationType = NotificationType.Success;

                    }
                    else
                    {
                        notification.Data = NotificationData.NO;
                        notification.Content = string.Format("角色:{0}删除失败", role.Name);
                        notification.NotificationType = NotificationType.Error;
                    }
                });

            }
            else
            {
                notification.Data = NotificationData.NO;
                notification.Content = string.Format("角色ID:{0}不存在！", id);
                notification.NotificationType = NotificationType.Attention;
            }

            return Result(notification);
        }

    }

}
